import fs from 'fs'
import express from 'express'
import React from 'react'
import { renderToString } from 'react-dom/server'
import { ContactContainer } from './app/components/contact'

const app = express()

app.set('views', './')
app.set('view engine', 'ejs')
app.use(express.static(`${__dirname}/public`))

const contacts = JSON.parse(fs.readFileSync(__dirname + '/public/contacts.json', 'utf8'))

const ContactContainerFactory = React.createFactory(ContactContainer)
console.log(contacts)
app.get('/', (req, res) => {
  const componentInstance = ContactContainerFactory({
    initialData: contacts,
  })
  res.render('index', {
    content: renderToString(componentInstance),
  })
})

app.listen(3000, () => {
  console.log('Express app listening on port 3000')
})